package proj.unions.general;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.KeyEvent;
import android.view.View;

public class GameView extends View implements Runnable{

	private Activity mActivity = null;
	private List<Info> mList = null;
//	private int mWidth = 0;
//	private int mHeight = 0;
	private int mIndex = 0;
	
	public GameView(Activity a, int resId[], String resName[]) {
		super(a);
		// TODO Auto-generated constructor stub
		mActivity = a;
		mList = new ArrayList<Info>();
		
//		mWidth = SuperTools.getInstance().getWidth();    
//		mHeight = SuperTools.getInstance().getWidth(); 

		mIndex = 0;
		if ( resId != null )
			for ( int i = 0; i < resId.length; i++ )
				mList.add(new Info(resId[i], null, null));
		
		if ( resName != null )
			for ( int i = 0; i < resName.length; i++ )
				mList.add(new Info(0, resName[i], null));
		
		setBitmap();
		new Thread(this).start();
	}

	public void nextBigmap()
	{
		if ( mIndex >= (mList.size() - 1) )
			mIndex = 0;
		else
			mIndex++;
	}
	
	@Override
	public void draw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.draw(canvas);
		Paint tpPaint = new Paint();
		tpPaint.setStyle(Paint.Style.FILL_AND_STROKE);
		canvas.drawBitmap(mList.get(mIndex).bitmap, 0, 0, tpPaint);
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while ( !Thread.currentThread().isInterrupted() )
		{
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Thread.currentThread().interrupt();
			}
			postInvalidate();
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		return super.onKeyDown(keyCode, event);
	}
	
	private boolean setBitmap()
	{
		try {
			
			for (Info i:mList)
			{
				InputStream in = null;
				if ( i.resId != 0 )
					in = getResources().openRawResource(i.resId);
				else if ( !i.resName.equals("") )
					in = mActivity.getAssets().open(i.resName);
				
				i.bitmap = getNewBitmap(BitmapFactory.decodeStream(in));
				in.close();
			}

		} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}
		
		return true;
	}
	
	public static Bitmap getNewBitmap(Bitmap tpBitmap)
	{
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		tpBitmap.compress(Bitmap.CompressFormat.PNG, 30, bos);
		try {
			bos.flush();
			bos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
		int bitmapWidth = tpBitmap.getWidth();
		int bitmapHeight = tpBitmap.getHeight();
			
		Matrix matrix = new Matrix();
		int width = SuperTools.getInstance().getWidth();    
		int height = SuperTools.getInstance().getHeight(); 
		float scaleX = width / (float)bitmapWidth;
		float scaleY = height / (float)bitmapHeight;
		matrix.postScale(scaleX, scaleY);
			
		tpBitmap = Bitmap.createBitmap(tpBitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);
		return tpBitmap;
	}

	public static Bitmap getNewBitmap(Bitmap tpBitmap, float _scaleX, float _scaleY)
	{
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		tpBitmap.compress(Bitmap.CompressFormat.PNG, 30, bos);
		try {
			bos.flush();
			bos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
		int bitmapWidth = tpBitmap.getWidth();
		int bitmapHeight = tpBitmap.getHeight();
		Matrix matrix = new Matrix();
		matrix.postScale(_scaleX, _scaleY);
			
		tpBitmap = Bitmap.createBitmap(tpBitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);
		return tpBitmap;
	}
	
	class Info{
		public int resId;
		public String resName;
		public Bitmap bitmap;
		Info(int tpResId, String tpResName, Bitmap tpBitmap)
		{
			resId = tpResId;
			resName = tpResName;
			bitmap = tpBitmap;
		}
	}
	
}
